package com.i_art_engineer.meigong.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.i_art_engineer.meigong.R;
import com.i_art_engineer.meigong.photo.operate.ImageObject;
import com.i_art_engineer.meigong.photo.operate.ImageOperateView;
import com.i_art_engineer.meigong.photo.operate.TextObject;
import com.i_art_engineer.meigong.util.DimenUtils;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by zoulinlin on 16/5/7.
 */
public class WaterMarkAdapter extends BaseAdapter{
    public JSONArray getImageArr() {
        return imageArr;
    }

    public void setImageArr(JSONArray imageArr) {
        this.imageArr = imageArr;
    }

    JSONArray imageArr = new JSONArray();

    private Context mContext;
    public  WaterMarkAdapter(Context context) {
        mContext= context;
    }



    @Override
    public int getCount() {
        return imageArr.length();
    }

    @Override
    public Object getItem(int position) {
        try {
            return imageArr.getJSONObject(position);
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setClickable(true);
            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            imageView.setBackgroundColor(0x80000000);

            int imgHeight = mContext.getResources().getDimensionPixelSize(R.dimen.photo_tools_watermark_list_height);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, imgHeight);
            int padding = DimenUtils.dp2px(mContext, 5);
            lp.setMargins(0, 0, 0, padding);
            imageView.setLayoutParams(lp);
        }
        else {
            imageView= (ImageView)convertView;
        }
        JSONObject jo =(JSONObject)getItem(position);
        final String sticker;
        try {
            sticker = jo.getString("img_url");
            imageView.setTag(sticker);
            Picasso.with(mContext)
                    .load(sticker)
                    .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
                    .noFade()
                    .into(imageView);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return imageView;
    }

}
